package com.gict.antifraudback.controller.web.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gict.antifraudback.entity.Config;
import com.gict.antifraudback.exception.MyException;
import com.gict.antifraudback.result.ResultEnum;
import com.gict.antifraudback.result.ResultView;
import com.gict.antifraudback.service.ConfigService;
import com.gict.antifraudback.vo.ConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ThinkPad
 */
@RestController
@Slf4j
@RequestMapping("/wapi/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping("/getList")
    public ResultView getList(ConfigVo configVo) {
        LambdaQueryWrapper<Config> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(configVo.getName()), Config::getName, configVo.getName());
        return ResultView.ok(configService.page(new Page<>(configVo.getCurrent(), configVo.getPageSize()), wrapper));
    }

    @GetMapping("/getTree")
    public ResultView getTree(String name) {
        LambdaQueryWrapper<Config> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(name), Config::getName, name);
        List<Config> list = configService.list(wrapper);

        return ResultView.ok(list);
    }

    @GetMapping("/get")
    public ResultView get(@Valid @NotNull Integer id) {
        Config config = configService.getById(id);
        if (config == null) {
            throw new MyException("配置不存在");
        }
        return ResultView.ok(config);
    }

    @PostMapping("/delete")
    public ResultView delete(@RequestBody ArrayList<Integer> ids) {
        log.info("configVo:{}", ids);
        for (Integer id : ids) {
            if (id == 1) {
                continue;
            }
            configService.removeById(id);
        }
        return ResultView.okMsg("删除成功");
    }

    @PostMapping("/add")
    public ResultView add(@Valid @RequestBody Config config) {
        this.createBefore(config);
        configService.save(config);
        return ResultView.ok();
    }

    /**
     * 创建前检查
     */
    private void createBefore(Config config) {
        LambdaQueryWrapper<Config> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Config::getName, config.getName());
        Config exists = configService.getOne(wrapper);

        if (exists != null && !exists.getId().equals(config.getId())) {
            throw new MyException(ResultEnum.CODE_2, "该配置已经存在");
        }
    }

    @PostMapping("/update")
    public ResultView update(@RequestBody @Valid Config config) {
        Config update = configService.getById(config.getId());
        if (update == null) {
            return ResultView.error(ResultEnum.CODE_2, "数据不存在！");
        }
        createBefore(config);
        configService.updateById(config);
        return ResultView.ok();
    }
}
